package com.edu.controller;

import com.edu.Result;
import com.edu.StatusCode;
import com.edu.pojo.EduDept;
import com.edu.service.EduDeptService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分院/班级控制层
 *
 * @author YJF
 */
@RestController
@RequestMapping("/dept")
public class EduDeptController {

    @Autowired
    private EduDeptService eduDeptService;

    /**
     * 查找所有的部门
     */
    @GetMapping("/all")
    public Result findAll() {
        List<EduDept> deptList = eduDeptService.findAll();
        return new Result(true, StatusCode.OK, "查询成功!", deptList);
    }

    /**
     * 分页条件查询所有部门
     */
    @GetMapping("/list")
    public Result findByPage(HttpServletRequest request) {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String query = request.getParameter("query");
        PageInfo<EduDept> deptList = eduDeptService.findByPage(page, size, query);
        return new Result(true, StatusCode.OK, "查询成功!", deptList);
    }

    /**
     * 添加部门
     */
    @PostMapping("/add")
    public Result addDept(@RequestBody EduDept eduDept) {
        int flag = eduDeptService.addDept(eduDept);
        if (flag == 1) {
            return new Result(true, StatusCode.OK, "添加成功!");
        } else if (flag == -1) {
            return new Result(false, StatusCode.REP_ERROR, "名称重复!");
        } else if (flag == 0) {
            return new Result(false, StatusCode.NOT_FIND, "未找到负责人信息!");
        }
        return new Result(false, StatusCode.ERROR, "未知错误!");
    }

    /**
     * 根据部门ID查找对应部门
     */
    @GetMapping("/one/{deptId}")
    public Result editDept(@PathVariable(value = "deptId") Integer deptId) {
        EduDept eduDept = eduDeptService.findById(deptId);
        return new Result(true, StatusCode.OK, "查询成功!", eduDept);
    }

    /**
     * 更新部门信息
     */
    @PutMapping("/update")
    public Result updateDept(@RequestBody EduDept eduDept) {
        int count = eduDeptService.update(eduDept);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "更新成功!", eduDept);
        }
        return new Result(false, StatusCode.ERROR, "更新错误!");
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/del/{deptId}")
    public Result deleteDept(@PathVariable(value = "deptId") Integer deptId) {
        int count = eduDeptService.delById(deptId);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "删除成功!");
        }
        return new Result(false, StatusCode.ERROR, "删除失败!");
    }

    /**
     * 分页查询班级列表
     */
    @GetMapping("/clss")
    public Result clsList(HttpServletRequest request) {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String parent = request.getParameter("deptParent");
        String cls = request.getParameter("deptClass");
        PageInfo<EduDept> clsList = eduDeptService.findClsPage(page, size, parent, cls);
        return new Result(true, StatusCode.OK, "查询成功!", clsList);
    }

    /**
     * 根据部门ID查找对应部门
     */
    @GetMapping("/cls/{deptId}")
    public Result findClsById(@PathVariable(value = "deptId") Integer deptId) {
        EduDept eduDept = eduDeptService.findClsById(deptId);
        return new Result(true, StatusCode.OK, "查询成功!", eduDept);
    }

    /**
     * 根据班级ID查找学生人数
     */
    @GetMapping("/stu/{deptId}")
    public Result findStuCount(@PathVariable(value = "deptId") Integer deptId) {
        int count = eduDeptService.findStuCount(deptId);
        return new Result(true, StatusCode.OK, "查询成功!", count);
    }

    /**
     * 查找所有的分院
     */
    @GetMapping("/allDept")
    public Result findAllDept() {
        List<EduDept> deptList = eduDeptService.findAllDept();
        return new Result(true, StatusCode.OK, "查询成功!", deptList);
    }

    /**
     * 根据分院ID查找班级
     */
    @GetMapping("/allCls/{deptId}")
    public Result findAllClsById(@PathVariable("deptId") Integer deptId) {
        List<EduDept> list = eduDeptService.findAllClsById(deptId);
        return new Result(true, StatusCode.OK, "查询成功!", list);
    }
}
